package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.admin.TbUser;
import com.dongdongshop.service.mail_quartz.MailQuartzService;
import com.dongdongshop.service.user.UserService;
import com.dongdongshop.utils.MailUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private MailQuartzService mailQuartzService;
    @Value("${redisSms.key}")
    private String key;
    @Reference
    private UserService userService;
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @RequestMapping("login")
    public String login(String username,String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return "login";
        }catch (IncorrectCredentialsException e){
            return "login";
        }
        return "redirect:/dongdongshop/index";
    }
    //注册页面
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    //发送短信
    @RequestMapping("sendVerificationCode")
    @ResponseBody
    public Result SendVerificationCode(String phoneNumber){
        rocketMQTemplate.convertAndSend("send-sms-code",phoneNumber);
        return Result.ok();
    }
    @RequestMapping("insertUser")
    @ResponseBody
    public Result insertUser(TbUser tbUser,String verificationCode){
        try {
            String code =  (String) redisTemplate.opsForHash().get(key, tbUser.getPhone());
            if (!verificationCode.equals(code)){
                return Result.fail().setData("验证码错误");
            }
            int i = userService.insertUser(tbUser);
            /*mailQuartzService.mailQuartz(tbUser.getUsername(),tbUser.getEmail());*/
            return Result.ok().setData("注册成功");
        }catch (Exception e){
            logger.error("注册失败",e);
            return Result.fail().setData("注册失败");
        }
    }
    @RequestMapping("forgotPassword")
    @ResponseBody
    public Result forgotPassword(String username){
        TbUser tbUser = userService.forgotPassword(username);
        mailUtil.sendSimpleMail(tbUser.getEmail(),"修改密码",tbUser.getPassword());
        return Result.ok();
    }
}
